<template>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    <el-form-item label="菜品名称" prop="fname">
      <el-input v-model="ruleForm.fname"></el-input>
    </el-form-item>
<!--    <el-form-item label="活动区域" prop="region">-->
<!--      <el-select v-model="ruleForm.region" placeholder="请选择活动区域">-->
<!--        <el-option label="区域一" value="shanghai"></el-option>-->
<!--        <el-option label="区域二" value="beijing"></el-option>-->
<!--      </el-select>-->
<!--    </el-form-item>-->

    <el-form-item label="菜品描述" prop="fdesc">
      <el-input v-model="ruleForm.fdesc"></el-input>
    </el-form-item>
    <el-form-item label="菜品价格" prop="price">
      <el-input v-model="ruleForm.price"></el-input>
    </el-form-item>
    <el-form-item label="菜品图片">
      <el-upload
        class="avatar-uploader"
        action="/api/foodUpload"
        :show-file-list="false"
        :on-success="handleAvatarSuccess"
        :before-upload="beforeAvatarUpload">
        <img v-if="ruleForm.image" :src="ruleForm.image"  class="avatar" v-model="ruleForm.image">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import axios from "axios";
    export default {
        name: "FoodUpdate",
        data() {
        return {
          ruleForm: {
            fid:'',
            fname: '',
            fdesc: '',
            price: '',
            image: ''
          },
          imageUrl: '',

          rules: {
            name: [
              { required: true, message: '请输入活动名称', trigger: 'blur' },
              { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
            ],

          }
        };
      },
      methods: {
        submitForm(formName) {
          this.$refs[formName].validate((valid) => {
            if (valid) {
              axios.post("/api/updateFood",this.ruleForm).then(res=>{
                if (res.data=="1"){
                  this.$router.push("/foodsList")
                }else{
                  alert("修改失败")
                }
              })
            } else {
              console.log('error submit!!');
              return false;
            }
          });
        },
        resetForm(formName) {
          this.$refs[formName].resetFields();
        },
        handleAvatarSuccess(res, file) {
          this.imageUrl = URL.createObjectURL(file.raw);
          this.ruleForm.image=res;
          alert(this.imageUrl)
        },
        beforeAvatarUpload(file) {
          const isJPG = file.type === 'image/jpeg';
          const isLt10M = file.size / 1024 / 1024 < 10;

          if (!isJPG) {
            this.$message.error('上传头像图片只能是 JPG 格式!');
          }
          if (!isLt10M) {
            this.$message.error('上传头像图片大小不能超过 10MB!');
          }
          return isJPG && isLt10M;
        }
      },
      mounted(){
        var fid =  this.$route.params.id
        //通过id 去后台进行查询数据
        // alert(fid)

        axios.get("/api/selectFoodById/"+fid).then(res=>{
          this.ruleForm=res.data;
        })


      },

    }
</script>

<style scoped>
  .avatar-uploader {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    width: 178px;
  }

  .avatar-uploader :hover {
    border-color: #409EFF;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }

  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
